package com.kotlin.use.viewmodel

import android.util.Log
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.kotlin.use.bean.AndroidRoot
import com.kotlin.use.repository.APIRepository
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class APIViewModel :ViewModel() {
    val userLiveData  = MutableLiveData<AndroidRoot>()

    fun requestLogin(username: String, password: String) {
        viewModelScope.launch {
                val response = withContext(Dispatchers.IO) {
                    APIRepository().requestLogin(username, password).execute()
                }
                if (response.isSuccessful) {
                    userLiveData.value = response.body()
                } else {
                    Log.d("APIViewModel", "没有请求到数据")
                }

        }
    }
}